package com.monitor.action.api;

import java.io.File;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.monitor.action.MonitorActionSupport;
import com.monitor.api.entity.Result;
import com.monitor.po.User;
import com.monitor.service.ApiService;
import com.monitor.util.CommenUtil;
import com.monitor.util.Constants;
import com.monitor.util.DateUtils;

public class UploadAction extends MonitorActionSupport {
	private static final long serialVersionUID = 8212809233055901326L;
	private File upload;
	private String uploadFileName;
	private String uploadContentType;
	private String phone;
	private String targetPhone;
	private long time;
	private boolean outgoing;
	private ApiService apiService;

	public void uploadAlbum() throws Exception {
		Result result = new Result();
		if (upload == null || uploadFileName == null || uploadContentType == null || CommenUtil.isEmpty(phone)) {
			result.setSuccess(false);
		} else {
			long fileSize = upload.length();
			if (fileSize <= 0) {
				result.setSuccess(false);
			} else {
				User user = apiService.getUserByPhone(phone);
				if (user != null && user.isVisible()) {
					String saveDir = getSaveDirName() + "/" + user.getId();
					// 获取保存路径
					String uploadDir = ServletActionContext.getServletContext().getRealPath(saveDir);
					// 创建路径
					File directory = new File(uploadDir);
					// 临时文件名称
					String tmpUploadimgFileName = DateUtils.getDateString(new Date(), Constants.DATEFORMAT_YYYY_MM_DD_HH_MM_SS_NO_SEPARATOR);

					// 重新设置文件名称
					uploadFileName = tmpUploadimgFileName + uploadFileName.substring(uploadFileName.indexOf("."));
					// 路径不存在时
					if (!directory.exists()) {
						// 创建不存在路径
						directory.mkdirs();
					}
					// 新建需要保存的新文件
					File dest = new File(uploadDir + "/" + uploadFileName);
					// 将上传文件保存至配置路径
					upload.renameTo(dest);
					// 得到圖片路徑
					String path = saveDir + "/" + uploadFileName;
					apiService.uploadAlbum(user.getId(), path, time);
					result.setSuccess(true);
					result.setUser(user);
				} else {
					result.setSuccess(false);
				}
			}
		}
		printJSONString(result);
	}

	public void uploadCallLog() throws Exception {
		Result result = new Result();
		if (upload == null || uploadFileName == null || uploadContentType == null || CommenUtil.isEmpty(phone)) {
			result.setSuccess(false);
		} else {
			long fileSize = upload.length();
			if (fileSize <= 0) {
				result.setSuccess(false);
			} else {
				User user = apiService.getUserByPhone(phone);
				if (user != null && user.isVisible()) {
					// 获取文件上传保存路径配置
					String saveDir = getSaveDirName() + "/" + user.getId();
					// 获取保存路径
					String uploadDir = ServletActionContext.getServletContext().getRealPath(saveDir);
					// 创建路径
					File directory = new File(uploadDir);
					// 临时文件名称
					String tmpUploadimgFileName = DateUtils.getDateString(new Date(), Constants.DATEFORMAT_YYYY_MM_DD_HH_MM_SS_NO_SEPARATOR);
					// 重新设置文件名称
					uploadFileName = tmpUploadimgFileName + uploadFileName.substring(uploadFileName.indexOf("."));
					// 路径不存在时
					if (!directory.exists()) {
						// 创建不存在路径
						directory.mkdirs();
					}
					// 新建需要保存的新文件
					File dest = new File(uploadDir + "/" + uploadFileName);
					// 将上传文件保存至配置路径
					upload.renameTo(dest);
					// 得到圖片路徑
					String path = saveDir + "/" + uploadFileName;
					apiService.uploadCallLog(user.getId(), targetPhone, time, path, outgoing);
					result.setSuccess(true);
					result.setUser(user);
				} else {
					result.setSuccess(false);
				}
			}
		}
		printJSONString(result);
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public long getTime() {
		return time;
	}

	public void setTime(long time) {
		this.time = time;
	}

	public ApiService getApiService() {
		return apiService;
	}

	public void setApiService(ApiService apiService) {
		this.apiService = apiService;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getTargetPhone() {
		return targetPhone;
	}

	public void setTargetPhone(String targetPhone) {
		this.targetPhone = targetPhone;
	}

	public boolean isOutgoing() {
		return outgoing;
	}

	public void setOutgoing(boolean outgoing) {
		this.outgoing = outgoing;
	}

}
